/**
 * Created by xding on 15/10/8.
 */

var CHttp = (function() {
    var constants = {
        HTTP_STATUS_SUCCESS: 200,
        HTTP_STATUS_FAIL: 500,
    }
    var _g = {};
    _g.getConstant = function(name){
        return constants[name];
    }
    return _g
})();


function write_obj(obj){
    var description = "";
    for(var i in obj){
        var property=obj[i];
        description+=i+" = "+property+"\n";
    }
    alert(description);
}


function fun_empty(obj)
{
    var type = typeof obj;
    switch (type)
    {
        case 'undefined':
            return true;
        case 'string':
            return obj.length == 0 ;
        case 'object':
            for (var i in obj)
            {
                return false;
            }
    }
    return true;
}


/**
 * post
 *
 * @param url
 * @param params
 * @returns {boolean}
 */
function fun_post(url,params)
{
    if (fun_empty(url) || fun_empty(params))
    {
        return false;
    }

    $.post(url, params, function(response){
        return response;
    },'json')
}


function fun_ajax(url,params) {
    if (fun_empty(url) || fun_empty(params))
    {
        return false;
    }

    var resp; // ajax callback return value

    $.ajax({
        url:        url,
        data:       params,
        async:      false, // 同步
        type:       'post',
        dataType:   'json',
        success: function (response) {
            resp = response;
        },
        error: function (response) {
            resp = false;
        }
    });

    return resp;
}